<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo-left">
        <img src="../assets/heima.png" alt="" />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logOut"> 退出 </el-button>
    </el-header>

    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧边栏区域 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">

        <!-- 左侧展开、折叠按钮 -->
        <div class="toggle-button" @click="toggleClick">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="secondaryPath"

        >
          <!-- 一级菜单 -->
          <el-submenu 
            :index=" item.id + ''" 
            v-for="item in menuList" 
            :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i  :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item 
            :index="'/' + subItem.path + ''" 
            v-for="(subItem, index) in item.children" 
            :key="subItem.id"
            @click="secondaryClick('/' + subItem.path + '')"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        
        </el-menu>
      </el-aside>

      <!-- 右侧边栏区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 引入axiso
import axios from 'axios'
import { menusApi } from '../request/api'
export default {
  data() {
    return {
        menuList:[],
        iconsObj:{
          '125':'iconfont icon-user',
          '101':'iconfont icon-shangpin',
          '102':'iconfont icon-danju',
          '103':'iconfont icon-tijikongjian',
          '145':'iconfont icon-baobiao',
        },
        iscollapse:false,
        secondaryPath:''
    }
  },
  created() {
    this.getMenuList()
    // 获取储存在sessionstorage中的当前菜单index 
    this.secondaryPath = window.sessionStorage.getItem('seconPath')
  },
  methods: {
    logOut() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },

    // 获取左侧边栏数据 （未进行axios封装时的写法）
    // async getMenuList() {
    //   const { data: res } = await this.$http.get('menus')
    //   if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    // },
    getMenuList() {
      menusApi().then((res) => {
        // console.log(res.data)
        if (res.data.meta.status !== 200)
          return this.$message.error(res.data.meta.msg)
          this.menuList = res.data.data
      })
    },
    // 展开折叠按钮
    toggleClick(){
      this.iscollapse = !this.iscollapse
    },
    // 点击二级菜单 保存连接的激活状态 (当前菜单的index储存在sessionstorage中)
    secondaryClick(pathparam){
      window.sessionStorage.setItem('seconPath',pathparam)
      this.secondaryPath = pathparam
    }
  },
}
</script>

<style lang="scss" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    color: white;
    span {
      margin-left: 10px;
    }
  }
}
.toggle-button{
  background:#4A5064;
  color: white;
  font-size: 10px;
  font-weight: bold;
  text-align: center;
  line-height: 24px;
  letter-spacing: 0.2em;
  cursor: pointer;
  padding: 2px;
}
.el-aside {
  background: #333744;
  .el-menu{
    border-right: none;
  }
}
.el-magin {
  background: #eaedf1;
}
.iconfont{
  margin-right: 10px;
}
</style>